$(document).ready(function(){
    // 显示添加
    $("#addbtn").click(function(){
        show_fwin("add_start");
    });
    // 关闭添加
    $("#closebtn").click(function(){
        $("#maskbg").hide();
        $("#fwin").hide();
        $("#add_start").show();
        $("#add_finish").hide();
    });
    // 创建文件夹
    $("#createdir").click(function(){
        $("#mycata").hide();
        $("#newcata").show();
    });
    // 取消创建文件夹
    $("#canceldir").click(function(){
        $("#mycata").show();
        $("#newcata").hide();
    });
    // 创建提交
    $("#adddir").click(function(){
        val = $('#newcata_name').val();
        val = $.trim(val);
        if (val == "") {
            $("#newcata_name").css({border:"1px solid #FF0000"});
            $("#newcata_name").focus();
            return false;
        }
        $.getJSON(addcata_ctrl, {newcata_name: val}, function(data){
            if (data.status && data.cid && data.cname) {
                $("#addput_cata").append("<option value='"+ data.cid +"' selected>"+ data.cname +"</option>");
                $("#mycata").show();
                $("#newcata").hide();
            } else {
                alert(data.message);
                return false;
            }
        });
    });
    // 添加提交
    $("#addForm").submit(function(){
        var url = $("#addput_url");
        var title = $("#addput_title");
        var cata = $("#addput_cata");
        if ($.trim(url.val()) == "" || !/((https|http|ftp|rtsp|mms)\:\/\/)?[\w\-]+\.[\w\-]+([\w\.\/\?\&\=\-\#]+)?/.test(url.val())) {
            url.css({border:"1px solid #FF0000"});
            url.focus();
            return false;
        } else {
            url.css({border:"1px solid #008000"});
        }
        if ($.trim(title.val()) == "") {
            title.css({border:"1px solid #FF0000"});
            title.focus();
            return false;
        } else {
            title.css({border:"1px solid #008000"});
        }

        $.ajax({
            type: "POST",
            url: addurl_ctrl,
            dataType:   "json",
            data: "url="+ url.val() +"&title="+ title.val() +"&cata="+ cata.val(),
            success: function(data){
                if (data.status == 1) {
                    $("#add_start").hide();
                    $("#add_finish").show();

                    $("#s_title").html(data.title);
                    $("#s_url").html(data.url);
                    $("#uid").val(data.id);
                    $('form').each(function(index){
                        $( "form" )[index].reset();
                    })
                } else {
                    alert(data.message);
                }
            }
        });
        return false;
    });

    // 完成提交
    $("#compForm").submit(function(){
        var uid = $("#uid");
        var intro = $("#addput_intro");
        $.post(saveintro_ctrl, {'id': uid.val(), 'intro': intro.val()});
        $("#closebtn").click();
        return false;
    });
    $("#cata_list").find("tr:gt(0)").each(function(){
        $(this).hover(function(){
            $(this).children("td:eq(1)").show();
            $(this).children("td").addClass("hover");
        }, function(){
            $(this).children("td:eq(1)").hide();
            $(this).children("td").removeClass("hover");
        });
    });
    $("#ulist").find("tr").each(function(){
        $(this).hover(function(){
            $(this).children("td").addClass('c-po-opt');
        }, function(){
            $(this).children("td").removeClass('c-po-opt');
        });
    });

    // 编辑完成提交
    $("#editForm").submit(function(){
        var url = $("#editput_url");
        var title = $("#editput_title");
        if ($.trim(url.val()) == "" || !/((https|http|ftp|rtsp|mms)\:\/\/)?[\w\-]+\.[\w\-]+([\w\.\/\?\&\=\-\#]+)?/.test(url.val())) {
            url.css({border:"1px solid #FF0000"});
            url.focus();
            return false;
        } else {
            url.css({border:"1px solid #008000"});
        }
        if ($.trim(title.val()) == "") {
            title.css({border:"1px solid #FF0000"});
            title.focus();
            return false;
        } else {
            title.css({border:"1px solid #008000"});
        }

        var uid = $("#editput_uid").val();
        $.ajax({
             type: "POST",
             url: editurl_ctrl,
             dataType: "json",
             data:   "uid="+ uid +"&url="+ $("#editput_url").val() +"&title="+ $("#editput_title").val() +"&intro="+ $("#editput_intro").val(),
             success: function(json){
                 cur_line = $("#u_" + uid);
                 cur_line.children("a").attr("href", json.url);
                 cur_line.children("a").html(json.title);
                 cur_line.children("p").html(json.intro);
             }
        });
        $("#closebtn").click();
        return false;
    });
});

function show_fwin(showid)
{
    $("#maskbg").show();
    $("#fwin").show();
    $("#add_start").hide();
    $("#add_finish").hide();
    $("#edit_box").hide();
    $("#"+showid).show();
}

function edit_url(id)
{
    show_fwin("edit_box");
    cur_line = $("#u_" + id);
    $("#editput_uid").val(id);
    $("#editput_url").val(cur_line.children("a").attr("href"));
    $("#editput_title").val(cur_line.children("a").html())
    $("#editput_intro").val(cur_line.children("p").html());
}

var org_chtml = '';
function cata_edit(item, id, name)
{
    org_chtml = $(item).parent().parent().html();
    $(item).parent().parent().html('<td colspan="2"><input type="text" size="30" name="ecata" id="ecata" class="textput" value="'+ name +'" onblur="cata_blur(this, '+ id +', \''+ name +'\')" /></td>');
    $("#ecata").select();
}
function cata_blur(item, id, name)
{
    var tr_obj = $(item).parent().parent();
    var new_name = $(item).val();
    tr_obj.html(org_chtml);
    if (new_name != name) {
        $.getJSON(editcata_ctrl, {'id': id, 'rename': new_name}, function(data){
            if (data.status == 1) {
                $(tr_obj).children('td:first').children('a:first').html(new_name);
            } else {
                alert(data.message);
            }
        });
    }
}
function cata_del(item, id, num)
{
    if (window.confirm("确定要删除这个目录吗？")) {
        if (num && window.confirm("这个目录下的"+ num +"条收藏也将被删除，确定要删除吗？")) {
            $.getJSON(delcata_ctrl, {'id': id}, function(data){
                if (data.status == 1) {
                    $(item).parent().parent().remove();
                } else {
                    alert(data.message);
                }
            });
        }
    }
}